<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>

    <!--在线引用JQuery-->
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"
          integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>

    <script>
        //检测用户名是否符合规范
        function checkUsername(obj) {
            let userName = $('#userName').val();
            if (userName.length < 4) {
                $('#userName').val('');
                $('#userPassword').val('');
                $('#checkUsername').text('您的账户名长度小于4位!').css('background', 'red');
            } else {
                $.ajax({
                    url: '/ajax/checkUsernameIsRepeat',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "userName": userName
                    },
                    success: function (result) {
                        if (result === true) {
                            //说明该用户名已经存在
                            $('#checkUsername').css('background-color', 'red').text('该用户名已经存在!');
                        } else {
                            $('#checkUsername').text('该用户名可以使用!').css('background', 'green');
                        }
                    }
                });
            }
        }

        //校验用户密码是否符合规范
        function checkUserPassword() {
            let userPassword = $('#userPassword').val();
            if (userPassword.length < 4) {
                $('#userName').val('');
                $('#userPassword').val('');
                $('#checkUserPassword').text('您的密码长度小于4位!').css('background', 'red');
            } else {
                $('#checkUserPassword').text('该密码可以使用!').css('background', 'green');
            }
        }

        //校验用户重复输入密码是否与之前密码吻合
        function checkUserPasswordRepeat() {
            let checkUserPasswordRepeat = $('#checkPassword').val();
            let userPassword = $('#userPassword').val();
            if (checkUserPasswordRepeat === userPassword) {
                $('#checkUserPasswordRepeat').text('两次输入密码一致!').css('background', 'green');
            } else {
                $('#checkUserPasswordRepeat').val('');
                $('#checkUserPasswordRepeat').text('两次输入密码不一致!').css('background', 'red');
            }
        }

        //用户注册
        function register() {
            $.ajax({
                url: '/ajax/register',
                type: 'post',
                dataType: 'json',
                data: {
                    'userName': $('#userName').val(),
                    'userPassword': $('#userPassword').val(),
                    'userEmail': $('#userEmail').val()
                },
                success: function (result) {
                    const register_message = result['register_message'];
                    if (register_message === '200') {
                        alert('注册成功,请前往邮箱激活!');
                        window.location.href = 'http://localhost:8080/';
                    } else if (register_message === '400') {
                        alert('由于网络原因,注册失败!');
                        window.location.href = 'http://localhost:8080/ajax/toRegister';
                    } else if (register_message === '500') {
                        alert('已经发送激活方式至您的邮箱!');
                        window.location.href = 'http://localhost:8080/ajax/toRegister';
                    }
                }
            });
        }
    </script>

    <style>
        #registerForm {
            border: 1px solid black;
            width: 400px;
            height: 300px;
            margin: auto;
        }

        #registerS {
            text-align: center;
        }
    </style>
</head>
<body>
<!--导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">个人图标</a>
        </div>


        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a th:href="@{/}">首页<span class="sr-only">(current)</span></a></li>
                <li><a href="#">咸鱼广场</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">咸鱼工作<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">IT</a></li>
                        <li><a href="#">金融</a></li>
                        <li><a href="#">服务</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">销售</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">土木</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="天安门招聘保安,熟手优先">
                </div>
                <button type="submit" class="btn btn-default">Get it</button>
            </form>

            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a th:href="@{/ajax/toLogin}" th:if="${userName==null}">登陆</a></li>
                <li><a th:href="@{/ajax/exit}" th:if="${userName!=null}">注销</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">我的账户 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<p align="center">欢迎注册!</p>
<form id="registerForm" onsubmit="return false" action="#" method="post">
    <br>
    <div id="registerS">
        <label for="userName"></label><input onblur="checkUsername()" id="userName" type="text" name="userName"
                                             required="required"
                                             placeholder="请输入您的用户名">
        <span id="checkUsername"></span><br><br>
        <label for="userPassword"></label><input onblur="checkUserPassword()" id="userPassword" type="password"
                                                 name="userPassword"
                                                 required="required" placeholder="请输入您的密码">
        <span id="checkUserPassword"></span><br><br>
        <label for="checkPassword"></label><input onblur="checkUserPasswordRepeat()" type="password" id="checkPassword"
                                                  required="required"
                                                  placeholder="请再次输入您的密码">
        <span id="checkUserPasswordRepeat"></span><br><br>
        <label for="userEmail"></label><input type="email" name="userEmail" id="userEmail" required="required"
                                              placeholder="请输入您的邮箱"><br>
        <button type="button" id="registerBtn" onclick="register()">注册</button>
    </div>
</form>
</body>
</html>